package com.saic.data.util;

import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.saic.data.table.TableUtil;

public class SimpleMail {
	
	// 设置FROM邮箱地址
	private static final String subPart = "163.com";// TODO
	private static final String fromAccount = "109353794";// TODO
	private static final String fromAddr = fromAccount + "@" + subPart;
	private static final String fromPasswd = "qasqasqas880408";// TODO
	private static final String emailHost = "smtp." + subPart;
	
	
	/*private static final String subPart = "saicstack.com";// TODO
	private static final String fromAccount = "dtoozie";// TODO
	private static final String fromAddr = fromAccount + "@" + subPart;
	private static final String fromPasswd = "Pass1q2w3e";// TODO
	private static final String emailHost = "mail."+subPart;//"smtp." + subPart*/
	
	/**
	 * @DESC:发送邮件
	 */
	public static void sendMail(final String dstAddress, final List<String> cList,final String subject,
			final String content)  {
		// 邮件相关设置
		Properties props = new Properties();
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.host", emailHost);// 根据目标邮箱修改
		props.setProperty("mail.port", "25");
		// 邮件协议
		TableUtil.pm("CheckSum-sendMail","open session");
		Session session = Session.getInstance(props, new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(fromAccount, fromPasswd);
			}
		});
		//session.setDebug(true);
		Message msg = new MimeMessage(session);
		TableUtil.pm("sendMail","send mail from "+fromAddr+" to "+dstAddress );
		try {
			msg.setFrom(new InternetAddress(fromAddr));
			msg.setSubject(subject);// TODO:写入邮件主题--可能会写死
			msg.setSentDate(new Date());
			msg.setContent("<span style='color:black'>" + content + "</span>",
					"text/html;charset=gbk");
			msg.setRecipients(RecipientType.TO, InternetAddress.parse(dstAddress));// 根据目标修改
			msg.setRecipients(RecipientType.CC, listToAddressArray(cList));
			Transport.send(msg);
		} catch (SendFailedException e) {
			// TODO Auto-generated catch block
			System.out.println("InvalidAddresses: "+getAddress(e.getInvalidAddresses()));
			System.out.println("ValidSentAddresses: "+getAddress(e.getValidSentAddresses()));
			System.out.println("ValidUnsentAddresses: "+getAddress(e.getValidUnsentAddresses()));
			e.printStackTrace();
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static String getAddress(Address[] a) {
		String validAddress = "";
		if(null!=a){
		 for(int i=0;i<a.length;i++)
		    {validAddress += a[i] + ",";}
		 if (validAddress.length()>=1) validAddress = validAddress.substring(0,validAddress.length()-1);
		 }
		return validAddress;
	}
	
	private static Address[] listToAddressArray(List<String> list) {
		Address[] array = new Address[list.size()];
		for (int i = 0; i < list.size(); i++) {
			String str = String.valueOf(list.get(i));
			Address address = null;
			try {
				address = new InternetAddress(str);
			} catch (AddressException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			array[i] = address;
		}
		return array;
	}




}
